package com.af.security.auth.util;

import com.af.security.auth.phone.PhoneAuthenticationException;
import com.af.security.auth.phone.PhoneUserDetails;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author : zhenyun.su
 * @comment :
 * @since : 2020-9-22
 */

public final class AfAuthenticationUtils {
    public static final String login_url = "/phone_login";
    public static final String login_method = "POST";
    public static final String session_phone_user_details = "szy_security_phone_user_details";
    public static final String session_phone_principal = "szy_security_principal";
    public static final String parameter_password = "password";
    public static final String parameter_phone = "phone";
    public static final String parameter_verification_code = "verificationCode";

    public static String obtainPassword(HttpServletRequest request) {
        String password = request.getParameter(parameter_password);
        if (password == null) {
            password = "";
        }
        return password.trim();
    }

    public static String obtainPhone(HttpServletRequest request) {
        String phone = request.getParameter(parameter_phone);
        if (phone == null) {
            phone = "";
        }
        return phone.trim();
    }
    public static String obtainVerificationCode(HttpServletRequest request) {
        String verificationCode = request.getParameter(parameter_verification_code);
        if (verificationCode == null) {
            verificationCode = "";
        }
        return verificationCode.trim();
    }

    public static PhoneUserDetails obtainUserDetails(HttpServletRequest request) {
        if (request.getSession() != null ){
            return (PhoneUserDetails) request.getSession().getAttribute(session_phone_user_details);
        }else{
            return null;
        }
    }
    public static void addUserDetails(HttpSession session, PhoneUserDetails userDetails) {
        if (session != null){
            session.setAttribute(session_phone_user_details, userDetails);
        }
    }

    public static void removeUserDetails(HttpServletRequest request) {
        if (request.getSession() != null ){
            request.getSession().removeAttribute(session_phone_user_details);
        }
    }

    /**
     * @comment : 将主体数据放入到session中， 页面可以读取主体数，主要菜单数据
     */
    public static void addPrincipal(HttpServletRequest request, Object principal) {
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute(session_phone_principal, principal);
        }
    }

    public static void removePrincipal(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session != null) {
            session.removeAttribute(session_phone_principal);
        }
    }

    /**
     * @comment :  验证短信验证码 - 下面验证顺序不能乱
     */
    public static void checkVerificationCode(PhoneUserDetails phoneUserDetails, String phoneRequest, String codeRequest) throws PhoneAuthenticationException {
        if (StringUtils.isEmpty(codeRequest)) {
            throw new PhoneAuthenticationException("alert.security.phone.code.required");
        }
        if (StringUtils.isEmpty(phoneRequest)) {
            throw new PhoneAuthenticationException("alert.security.phone.required");
        }
        if (phoneUserDetails == null) {
            throw new PhoneAuthenticationException("alert.security.phone.userDetails.not-exist");
        }
        if (!phoneUserDetails.getPhone().equals(phoneRequest)) {
            throw new PhoneAuthenticationException("alert.security.phone.not-match");
        }
        if (phoneUserDetails.expired()) {
            throw new PhoneAuthenticationException("alert.security.phone.code.expired");
        }
        if (!phoneUserDetails.getVerificationCode().equals(codeRequest)) {
            throw new PhoneAuthenticationException("alert.security.phone.code.not-match");
        }
    }
}
